﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skyhook.AI.PathFinder
{
    class PointDataSorter
    {
        PointData[] Data = new PointData[50000];
        PointData FirstPointData = null;

        int MinData = 1000001;
        
        public PointDataSorter()
        {
            for (int i = 0; i < Data.Length; i++)
            {
                Data[i] = null;
            }
        }
        public void AddPointData(PointData data)
        {
            /*data.Next = FirstPointData;
            if (FirstPointData != null)
            {
                FirstPointData.Values.Previous = data;
            }
            FirstPointData = data;*/
            data.NextSorter = null;
            data.PreviousSorter = null;

            data.NextSorter = FirstPointData;
            if (FirstPointData != null)
            {
                FirstPointData.PreviousSorter = data;
            }
            FirstPointData = data;

            if (Data[data.F] == null)
            {
                Data[data.F] = data;
                data.NextMinF = null;
                data.PreviousMinF = null;
            }
            else
            {
                data.NextMinF = Data[data.F];
                Data[data.F].PreviousMinF = data;
                data.PreviousMinF = null;
                Data[data.F] = data;
            }
            
            if (data.F < MinData)
            {
                MinData = data.F;
            }
        }

        public void RemovePointData(PointData data)
        {
            if (data.NextSorter != null)
            {
                data.NextSorter.PreviousSorter = data.PreviousSorter;
            }
            if (data.PreviousSorter != null)
            {
                data.PreviousSorter.NextSorter = data.NextSorter;
            }

            if (FirstPointData == data)
            {
                FirstPointData = data.NextSorter;
            }

            PointData curpoint = Data[data.F];
            while (curpoint != data)
            {
                curpoint = curpoint.NextMinF;
            }

            if (curpoint.PreviousMinF == null && curpoint.NextMinF == null)
            {
                Data[data.F] = null;
            }
            if (curpoint.PreviousMinF == null && curpoint.NextMinF != null)
            {
                curpoint.NextMinF.PreviousMinF = null;
                Data[data.F] = curpoint.NextMinF;
            }
            if (curpoint.PreviousMinF != null && curpoint.NextMinF == null)
            {
                curpoint.PreviousMinF.NextMinF = null;
            }
            if (curpoint.PreviousMinF != null && curpoint.NextMinF != null)
            {
                curpoint.PreviousMinF.NextMinF = curpoint.NextMinF;
                curpoint.NextMinF.PreviousMinF = curpoint.PreviousMinF;
            }
            if (MinData == data.F)
            {
                curpoint = Data[MinData];
                while (curpoint == null && MinData < Data.Length - 1)
                {
                    MinData++;
                    curpoint = Data[MinData]; 
                }
            }

            data.NextMinF = null;
            data.PreviousMinF = null;
            data.NextSorter = null;
            data.PreviousSorter = null;
        }

        public PointData GetMinPointData()
        {
            /*PointData curpoint = FirstPointData;
            PointData result = null;
            int minf = 1000000;
            for (; curpoint != null; curpoint = curpoint.NextSorter)
            {
                if (curpoint.F < minf)
                {
                    minf = curpoint.F;
                    result = curpoint;
                }
            }
            return result;*/

            /*int pos = 0;
            while (pos < Data.Length && Data[pos] == null)
            {
                pos++;
            }

            if (Data[pos] != null)
            {
                return Data[pos];
            }

            return null;*/

            return Data[MinData];
        }

        public void Clear()
        {
            /*if (FirstPointData != null && FirstPointData.NextMinF != null)
            {
                FirstPointData.NextMinF.Values.Previous = null;
                FirstPointData.NextMinF = null;
            }
            FirstPointData = null;*/

            while(FirstPointData != null)
            {
                Data[FirstPointData.F] = null;
                FirstPointData = FirstPointData.NextSorter;
            }

            FirstPointData = null;

            MinData = 1000001;
        }
    }
}
